(function ($) {
    $.fn.extend({
        reg() {

            $("input:checkbox").on("click", function () {
                $("form").changeColor();
            })



            //自定义规则
            // $.validator.addMethod("规则名",函数) 返回true和fasle
            $.validator.addMethod("checkName", (value, element, param) => {

                return /^[a-z]\w+/ig.test(value);
            })
            $.validator.addMethod("checkPwd", (value, element, param) => {

                return /^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)])+$)/g.test(value);
            })

            $("form").validate({
                //rules 规则
                rules: {
                    uname: {
                        required: true,//用户名必填
                        checkName: true, //开启自定义的规则
                        rangelength: [6, 18], //长度6-18位
                        remote: {
                            type: "get",
                            url: "/api/checkName"
                        }    
                    },
                    upwd: {
                        required: true,
                        checkPwd: true,
                        rangelength: [8, 16]
                    },
                    upwd2: {
                        equalTo: "#upwd"
                    },
                    uphone: {
                        rangelength: [11, 11],
                        digits: true
                    },
                    uemail: { //	email字段
                        email: true // 	email规则
                    }
                },
                //messages 提示消息
                messages: {
                    uname: {
                        required: "用户名必填",
                        checkName: "用户名不合法",
                        rangelength: '用户名长度必须{0}-{1}',
                        remote: "该用户名已注册!"
                    },
                    upwd: {
                        required: "密码必填",
                        rangelength: '长度必须{0}-{1}',
                        checkPwd: "大小写字母、数字和特殊符号的组合"
                    },
                    upwd2: {
                        equalTo: "2次密码不一致"
                    },
                    uphone: {
                        rangelength: "手机号必须11位",
                        digits: "只能是数字"
                    },
                    uemail: { //	email字段
                        email: "邮箱不合法" // 	email规则
                    }
                },



                //等价于 onsubmit
                submitHandler() {
                    //ajax...
                    $('#reg').prop("disabled", true);

                    var index = layer.load(1, {
                        shade: [0.1, '#fff'] //0.1透明度的白色背景
                    });

                    $.ajax({
                        url: "/api/reg",
                        type: 'post',
                        data: $("form").serialize(),
                    }).then(function (res) {
                        $('#reg').prop("disabled", false);
                        layer.close(index);
                        if (res && res.code == 200) {
                            layer.confirm("恭喜,注册成功!,是否立即去登录", {
                                btn: ["确定", "取消"]
                            }, function (tmpIndex) {
                                //由于点了确定.不会自动关闭确认框,需要
                                //手动关闭
                                layer.close(tmpIndex);

                                setTimeout(function () {
                                    location.href = './login.html';
                                }, 500);

                            }, function () {
                                //取消 会自动关闭窗口
                            });
                        }
                    })

                    return false
                }
            })

        },
        changeColor() {
            if ($("input:checkbox").prop("checked") == true) {
                $("button").css("backgroundColor", "red");
                $("#login").prop("disabled", false);
            } else {
                $("button").css("backgroundColor", "#999");
                $("#login").prop("disabled", true)
            }
        }
    })
})(jQuery)
$(function () {
    $(window).reg();
})